package net.asg.games.yokeltowers.screens;

import java.util.Iterator;

import net.asg.games.yokeltowers.LevelManager;
import net.asg.games.yokeltowers.YokelTowersGame;
import net.asg.games.yokeltowers.models.YokelGameBoard;
import net.asg.games.yokeltowers.view.ColumnYokelGameObject;
import net.asg.games.yokeltowers.view.YokelGameObject;
import net.asg.games.yokeltowers.view.YokelGameObjectFactory;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.TimeUtils;

/**
 * @author Blakbro2k
 * 
 *         The main screen for the game. This screen is where the main game is
 *         played. All sprites and game objects are drawn here. This screen
 *         extends <code>CommonScreen</code> to minimize having to write
 *         unimplemented methods.
 */
public class GameScreen extends CommonScreen {
	final private int NEXT_BOX_X = 322; //322
	final private int NEXT_BOX_Y = 306; //306
	final private int START_X = 341; //375
	final private int START_Y = 356;
	final private int FLOOR = 130;
	final private int LEFT_WALL = 341;
	final private int RIGHT_WALL = 442;

	protected Texture backgroundImage;

	protected Texture foregroundImage;

	protected Rectangle boardBounds;

	protected YokelGameObject nextColumn;

	protected YokelGameObject currentColumn;

	private YokelGameObjectFactory yokelGameObjectFactory;

	private Array<YokelGameObject> movingObjects;

	private LevelManager levelManager;

	private BitmapFont bitmapFontName;
	
	private YokelGameBoard gameboard;

	public GameScreen(YokelTowersGame game) {
		this.game = game;
	}

	public void show() {
		imageProvider = game.getImageProvider();
		soundManager = game.getSoundManager();
		soundManager.setSoundOn(true);

		backgroundImage = imageProvider.getBackground();
		debugRenderer = new ShapeRenderer();

		camera = new OrthographicCamera();
		camera.setToOrtho(false, imageProvider.getScreenWidth(),
				imageProvider.getScreenHeight());

		batch = new SpriteBatch();

		yokelGameObjectFactory = new YokelGameObjectFactory(imageProvider,
				soundManager);

		movingObjects = new Array<YokelGameObject>();
		bitmapFontName = new BitmapFont();

		levelManager = new LevelManager(true);
		
		gameboard = new YokelGameBoard();

		currentColumn = levelManager.getNextObject(yokelGameObjectFactory);
		nextColumn = levelManager.getNextObject(yokelGameObjectFactory);

		nextColumn.setX(NEXT_BOX_X);
		nextColumn.setY(NEXT_BOX_Y);

		currentColumn.setX(START_X);
		currentColumn.setY(START_Y);

		boardBounds = new Rectangle();
		boardBounds.width = RIGHT_WALL - LEFT_WALL;
		boardBounds.height = START_Y - FLOOR;
		boardBounds.x = NEXT_BOX_X + 4 + 16;
		boardBounds.y = FLOOR;
		
		Gdx.input.setInputProcessor(this);
		Gdx.input.setCatchBackKey(true);
	}

	@Override
	public void render(float delta) {
		Gdx.gl.glClearColor(1, 1, 1, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

		camera.update();
		batch.setProjectionMatrix(camera.combined);

		if (game.isDebugOn) {
			debugRenderer.setProjectionMatrix(camera.combined);
			debugRenderer.begin(ShapeType.Rectangle);
			debugRenderer.setColor(new Color(0, 1, 0, 1));
		}

		batch.begin();
		batch.draw(backgroundImage, 0, 0);
		
		gameboard.drawBoard(batch);

		nextColumn.draw(batch);
		currentColumn.draw(batch);

		if (game.isDebugOn) {
			nextColumn.drawDebug(debugRenderer);
			currentColumn.drawDebug(debugRenderer);
			debugRenderer.rect(boardBounds.x, boardBounds.y, boardBounds.width, boardBounds.height);
		}

		batch.end();

		if (game.isDebugOn) {
			debugRenderer.end();
		}

		//process key imports
		processInput(delta);

		//Move objects down
		if (levelManager.state.equals(YokelGameObject.ObjectState.FALLING)){
			currentColumn.moveDown(delta, 1, boardBounds);
		}
	}

	private void processInput(float delta) {
		if (Gdx.input.isKeyPressed(Keys.DOWN)) {
			currentColumn.moveDown(delta, levelManager.getSpeedBonus(), boardBounds);
		}
		if (Gdx.input.isKeyPressed(Keys.LEFT)) {
			currentColumn.moveLeft(delta, 1, boardBounds);
		}
		if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
			currentColumn.moveRight(delta, 1, boardBounds);
		}
	}

	@Override
	public boolean keyUp(int keycode) {
		if (keycode == Keys.ESCAPE || keycode == Keys.BACKSPACE) {
			Gdx.app.exit();
			return true;
		}
		
		if (keycode == Keys.UP) {
			((ColumnYokelGameObject) currentColumn).cycleDown();
			return true;
		}
		return false;
	}
}
